<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in EmployeesList"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeesSimple } from '@/api/employees'
import { addDepartments, getdataDepartments, updataDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    orginList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 这是name重复性的验证
    const vaildname = (rule, value, callback) => {
      // 这一段代码可以去掉自己的输入框里面的数据  不去除会导致一直数组里面有这个东西
      let existNameList = this.orginList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const curDepartment = this.orginList.find(item => item.id === this.id)
        const pid = curDepartment.pid
        const brothers = this.orginList.filter(item => item.pid === pid)
        existNameList = brothers.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (existNameList.includes(value)) {
        callback(new Error(`部门:${value}已经存在了`))
      } else {
        callback()
      }
    }
    // 这是code重复性的验证
    const vaildCode = (rule, value, callback) => {
      console.log(existCodeList, 'existCodeList')
      let existCodeList = this.orginList.map(item => item.code)
      // 这一段代码可以去掉自己的输入框里面的数据  不去除会导致一直数组里面有这个东西
      if (this.isEdit) {
        existCodeList = this.orginList.filter(item => item.id !== this.id).map(item => item.code)
      }
      console.log(existCodeList, 'existCodeList')
      if (existCodeList.includes(value)) {
        callback(new Error(`部门编码:${value}已经存在了`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 负责人列表
      EmployeesList: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1~50的字符', trigger: 'blur' },
          { validator: vaildname, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1~50的字符', trigger: 'blur' },
          { validator: vaildCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门管理', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1~300的字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployeesSimple()
    if (this.isEdit) {
      this.loaddataDepartments()
    }
  },
  methods: {
    resetFrom() {
      this.$refs.deptForm.resetFields()
    },
    // 获取当前部门的数据
    async  loaddataDepartments() {
      try {
        const res = await getdataDepartments(this.id)
        console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取员工简单信息
    async loadEmployeesSimple() {
      try {
        const res = await getEmployeesSimple()
        // console.log(res.data)
        this.EmployeesList = res.data
        // console.log(this.EmployeesList)
      } catch (error) {
        console.log(error)
      }
    },
    // 确定
    hSubmit() {
      // this.doAdd()
      // 通过父组件里面传送过来的是ture还是false 来判断是进行添加还是编辑操作
      this.$refs.deptForm.validate(vaild => {
        if (!vaild) {
          return
        }
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 编辑
    async doEdit() {
      try {
        const res = await updataDepartments(this.form)
        console.log(res)
        if (res.code !== 10000) {
          return this.$message.error(res.message)
        }
        //  添加成功换到父组件上面渲染数据
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    async doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
        if (res.code !== 10000) {
          return this.$message.error(res.message)
        }
        //  添加成功换到父组件上面渲染数据
        this.$emit('success')
      } catch (error) {
        console.log(error)
        return this.$message.error(error)
      }
    },
    // 取消
    hCancel() {}
  }
}
</script>
